<script setup lang="ts">
import { deleteSCJ, findAll, update } from "@/api/favorites";

import { ElContainer, ElHeader, ElMain, FormInstance } from "element-plus";
import { cloneDeep } from "lodash";
import { useUserStore } from "@/store";

const tableData = ref([]);
const loading = ref<boolean>(false);
const tableRef = ref();
const searchFormRef = ref<FormInstance | null>(null);
const dialogVisible = ref<boolean>(false);
const favorites = ref<Favorites>();

//双向绑定的对象
const formData = ref(cloneDeep(favorites));

//添加或编辑框中，用到的对象属性
const DEFAULT_FORM_DATA = {
  id: undefined,
  username: "",
  password: "",
  code: "",
  nick_name: "",
  sex: "",
  birthday: "",
  major_id: "",
  major_name: "",
  head: "",
  department_name: "",
};
//获取 添加/编辑 页面中的表单
const formRef = ref<FormInstance | null>(null);
//重置表单内容
const resetForm = () => {
  formRef.value?.clearValidate();
  formData.value = cloneDeep(DEFAULT_FORM_DATA);
};

//编辑按钮绑定的事件
const handleUpdate = (row: TableData) => {
  dialogVisible.value = true;
  formData.value = cloneDeep(row);
};

const getTableData = () => {
  loading.value = true;
  console.log("aaaa");
  const userStore = useUserStore();
  const userid = userStore.user.userId;
  findAll(userid)
    .then((res) => {
      console.log("bbbb", res);
      tableData.value = res;
    })
    .finally(() => {
      loading.value = false;
    });
};

//修改框中，“确定”按钮绑定的事件
const handleCreateOrUpdate = () => {
  formRef.value?.validate((valid: boolean, fields) => {
    loading.value = true;
    console.log("aaa", formData.value);
    update(formData.value)
      .then(() => {
        ElMessage.success("操作成功");
        window.location.reload();
        dialogVisible.value = false;
      })
      .finally(() => {
        loading.value = false;
      });
  });
};

//删除按钮绑定的事件
const handleDelete = (favorite: any) => {
  ElMessageBox.confirm(`正在删除视频：${favorite.title}，确认删除？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    console.log("aaaa", favorite);
    deleteSCJ(favorite).then(() => {
      ElMessage.success("删除成功");
      window.location.reload();
    });
  });
};

const headRef = ref();
const handlerUploadSuccess = (res: any) => {
  console.log(res);
  formData.value = res;
  headRef.value.clearFiles();
};
onMounted(() => {
  getTableData();
});
</script>

<template>
  <div class="h-full2">
    <h1>我的收藏夹</h1>
    <div class="my-Video">
      <el-table :data="tableData" ref="tableRef">
        <el-table-column prop="cover" label="收藏夹封面" align="center">
          <template #default="scope">
            <el-avatar
              :src="`http://localhost:8080/api/images/${scope.row.head}`"
            />
          </template>
        </el-table-column>
        <el-table-column prop="title" label="收藏夹标题" align="center" />
        <el-table-column prop="description" label="收藏夹简介" align="center" />
        <el-table-column prop="count" label="视频数量" align="center" />
        <el-table-column prop="type" label="收藏夹类型" align="center" />
        <el-table-column fixed="right" label="操作" width="150" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              text
              bg
              size="small"
              @click="handleUpdate(scope.row)"
            >
              修改
            </el-button>
            <el-button
              type="danger"
              text
              bg
              size="small"
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 修改 -->
      <el-dialog v-model="dialogVisible" @closed="resetForm" width="30%">
        <el-form
          ref="formRef"
          :model="formData"
          label-width="100px"
          label-position="left"
        >
          <el-form-item prop="coverUrl" label="收藏夹封面">
            <el-upload
              ref="headRef"
              class="upload-demo"
              action="/api/edu/upload"
              name="file"
              :show-file-list="false"
              :on-success="handlerUploadSuccess"
            >
              <el-button type="primary">请选择收藏夹封面</el-button>
              <el-avatar
                :src="`http://localhost:8080/api/images/${formData.cover}`"
              />
            </el-upload>
          </el-form-item>
          <el-form-item prop="title" label="收藏夹标题">
            <el-input v-model="formData.title" placeholder="请输入" />
          </el-form-item>
          <el-form-item prop="descr" label="收藏夹简介">
            <el-input v-model="formData.description" placeholder="请输入" />
          </el-form-item>
          <el-form-item prop="descr" label="收藏夹类型">
            <el-input v-model="formData.type" placeholder="请输入" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="handleCreateOrUpdate"
            :loading="loading"
          >
            确认
          </el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<style scoped lang="scss">
.common-layout {
  height: 200px;
  width: 100%;
}
.h-full1 {
  height: 200px;
  width: 100%;
  background-color: #f1f2f6;
}
.h-full2 {
  height: 200px;
  width: 100%;
  background-color: #c8c9e6;
}
.h-full3 {
  height: 200px;
  width: 100%;
  background-color: #3f405c;
}
.h-full4 {
  height: 200px;
  width: 100%;
  background-color: #5156c3;
}
</style>
